package com.ruoyi.web.controller.activities;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.domain.DailyActivity;
import com.ruoyi.system.service.IDailyActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 创客动态Controller
 * 
 * @author ruoyi
 * @date 2024-12-05
 */
@Anonymous
@RestController
@RequestMapping("/activities/activity")
@Api("动态页面接口")
public class DailyActivityController extends BaseController
{
    @Autowired
    private IDailyActivityService dailyActivityService;

    /**
     * 查询创客热点
     */

    @ApiOperation("热点接口")
    @Anonymous
    @GetMapping("/getHotActivities")
    public AjaxResult getHotActivities() {
        // 查询 top 不为空的数据并按照 top 字段升序排列
        List<DailyActivity> hotActivities = dailyActivityService.selectHotActivities();
        return success(hotActivities);
    }


    /**
     * 查询创客动态列表
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(DailyActivity dailyActivity)
    {
        startPage();
        List<DailyActivity> list = dailyActivityService.selectDailyActivityList(dailyActivity);
        return getDataTable(list);
    }

    /**
     * 导出创客动态列表
     */

    @Log(title = "创客动态", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DailyActivity dailyActivity)
    {
        List<DailyActivity> list = dailyActivityService.selectDailyActivityList(dailyActivity);
        ExcelUtil<DailyActivity> util = new ExcelUtil<DailyActivity>(DailyActivity.class);
        util.exportExcel(response, list, "创客动态数据");
    }

    /**
     * 获取创客动态详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:post:query')")
    @GetMapping(value = "/{activityId}")
    public AjaxResult getInfo(@PathVariable("activityId") String activityId)
    {
        return success(dailyActivityService.selectDailyActivityByActivityId(activityId));
    }

    /**
     * 新增创客动态
     */

    @PreAuthorize("@ss.hasPermi('system:post:add')")
    @Log(title = "创客动态", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DailyActivity dailyActivity)
    {
        return toAjax(dailyActivityService.insertDailyActivity(dailyActivity));
    }

    /**
     * 修改创客动态
     */

    @PreAuthorize("@ss.hasPermi('system:post:edit')")
    @Log(title = "创客动态", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DailyActivity dailyActivity) {
        // 检查 top 是否为 null
        if (dailyActivity.getTop() == null) {
            // 如果 top 是 null，可以选择不做任何操作，或者设置默认值
            // 比如： dailyActivity.setTop(null); // 将其设置为 null
            // 或者设置默认值， 例如： dailyActivity.setTop(0); // 设置为 0，具体视业务需求
        }

        // 更新创客动态
        int rows = dailyActivityService.updateDailyActivity(dailyActivity);
        return toAjax(rows);
    }


    /**
     * 删除创客动态
     */
    @PreAuthorize("@ss.hasPermi('system:post:remove')")
    @Log(title = "创客动态", businessType = BusinessType.DELETE)
	@DeleteMapping("/{activityIds}")
    public AjaxResult remove(@PathVariable String[] activityIds)
    {
        return toAjax(dailyActivityService.deleteDailyActivityByActivityIds(activityIds));
    }
}
